package api

import (
	"awesomeProject2/global"
	"awesomeProject2/model"
	"awesomeProject2/model/request"
	"awesomeProject2/model/response"
	"awesomeProject2/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
)

func CreateProject(c *gin.Context) {
	var project model.Project
	var annualProjectRecord model.AnnualProjectRecord
	var data request.Project
	_ = c.ShouldBindJSON(&data)

	// 将request中的字段赋值到Project 与AnnualProjectRecord
	service.FillProject(c, &project, &annualProjectRecord, &data)

	if err := service.CreateProject(&project, &annualProjectRecord); err != nil {
		global.GVA_LOG.Error("新增项目失败!", zap.Any("err", err))
		response.FailWithError(err, c)
	} else {
		response.Ok(c)
	}
}
func GetProject(c *gin.Context) {
	prid, _ := strconv.Atoi(c.Param("id"))
	if err, reproject := service.GetProject(uint(prid)); err != nil {
		response.FailWithError(err, c)
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data": reproject,
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		},
		)
	}
}
func GetProjects(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	userid, _ := strconv.Atoi(c.Query("userid"))
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = 1
	}
	if err, project, _ := model.GetProjects(uint(userid), pageSize, pageNum); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "查询失败", "status": http.StatusNotFound})

	} else {
		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"projectList": project},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		})
	}
}

func UpdateProject(c *gin.Context) {
	var project model.Project
	var annualProjectRecord model.AnnualProjectRecord
	var data request.Project
	_ = c.ShouldBindJSON(&data)

	// 将request中的字段赋值到Project 与AnnualProjectRecord
	service.FillProject(c, &project, &annualProjectRecord, &data)

	if err := service.UpdateProject(&project, &annualProjectRecord); err != nil {
		global.GVA_LOG.Error("更新项目失败!", zap.Any("err", err))
		response.FailWithError(err, c)
	} else {
		response.Ok(c)
	}

}
func DeleteProject(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))

	if err := service.DeleteProject(id); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "删除失败", "status": http.StatusNotFound})

	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "删除成功",
				"status": http.StatusOK,
			},
		})
	}
}
func GetProjectByLevel(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	projectlevel := c.Query("projectLevel")
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = 1
	}
	if err, projects, total := model.GetProjectByLevel(projectlevel, pageSize, pageNum); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "删除失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  gin.H{"projectList": projects},
			"tatal": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}

// GetAllOngoingProjects 获取在研项目
func GetAllOngoingProjects(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	//projectlevel := c.Query("projectLevel")
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = 1
	}
	if err, projects, total := model.GetAllOngoingProject(pageSize, pageNum); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "删除失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  gin.H{"projectList": projects},
			"tatal": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}
func ChangeProjectVisiblity(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := model.ChangeProjectVisiblity(uint(id)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "更新失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "更新成功",
				"status": http.StatusOK,
			},
		})
	}
}

func GetAllProjects(c *gin.Context) {
	userid, _ := c.Get("userid")
	year, _ := strconv.Atoi(c.Param("year"))

	if err, data, tt := service.GetAllProjects(userid.(uint), year); err != nil {
		response.FailWithError(err, c)
	} else {

		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"projectList": data, "total": tt},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		})
	}
}

func GetAllProjectsbq(c *gin.Context) {
	userid, _ := c.Get("userid")
	year, _ := strconv.Atoi(c.Param("year"))

	if err, data, tt := service.GetAllProjectsbq(userid.(uint), year); err != nil {
		response.FailWithError(err, c)
	} else {

		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"projectList": data, "total": tt},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		})
	}
}

func GetProjectsDynamic(c *gin.Context) {
	var searchInfo request.ProjectSearchInfo
	_ = c.ShouldBindQuery(&searchInfo)
	if err, list, total := service.GetProjectsDynamic(searchInfo); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.Fail(c)
	} else {
		response.OkWithData(response.PageResult{
			List:  list,
			Total: total,
		}, c)
	}
}

func GetAllProjectsWithoutyear(c *gin.Context) {

	userid, _ := c.Get("userid")

	if err, data, tt := model.GetAllProjects3(userid.(uint)); err != nil {

		c.JSON(http.StatusOK, gin.H{"msg": userid.(uint), "status": http.StatusNotFound})
	} else {

		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"projectList": data, "total": tt},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		})
	}
}
